package com.cloudknow.Base;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;


import com.cloudknow.common.CommonConstant;

import com.cloudknow.utils.PrefsUtil;
import com.cloudknow.utils.StringUtils;

import java.util.Map;

public abstract class BasePresenter<T> {

    protected T view;
    protected Context context;

    public BasePresenter() {
    }

    /**
     * 显示一个对话框
     */
    public AlertDialog showPositveAlerDialog(String pTitle, String message, DialogInterface.OnClickListener pOkClickListener) {
        AlertDialog mAlertDialog = new AlertDialog.Builder(context)
                .setTitle(pTitle).setMessage(message)
                .setPositiveButton("确定", pOkClickListener)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .show();
        return mAlertDialog;
    }

    /**
     * 是否要去登录，true要去登录，false不用去登录
     */
    private boolean isLogin(Map<String, Object> paramsMap) {
        if (paramsMap != null) {
            for (String key : paramsMap.keySet()) {
                if (key.equals("user_id")) {// 接口中有user_id的字段，判断有没有userid，没有就去登录
                    String userId = PrefsUtil.getString(context, CommonConstant.USER_ID);
                    if (StringUtils.isNull(userId)) {// userId为空
                        showPositveAlerDialog("提示", "是否去登陆？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // TODO: 2019/1/5 调转到loginActivity登录页
                            }
                        });
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
        return false;
    }

    public void attachView(T view, Context context) {
        this.view = view;
        this.context = context;

    }


}
